package com.lemon.smartsafetynew.ui.activity.record;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafetynew.entity.record.GroupMajorHazardvo;
import com.lemon.smartsafetynew.entity.record.MajorHazardVo;
import com.lemon.smartsafetynew.net.services.RecordServices;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.ui.adapter.record.MajorExpandaleAdapter;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 重大危险源详情
 * Created by JiangAn on 2018/5/28.
 */

public class RecordMajorNewDetailActivity extends SwipeBackActivity {

    @Bind(R.id.iv_return)
    ImageView ivReturn;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.expand_list)
    ExpandableListView expandList;

    private MajorExpandaleAdapter adapter;
    private static MajorHazardVo data;
    private Boolean isShowReturn = false;
    private String companyId;
    List<GroupMajorHazardvo> mDataList;
    @Override
    protected void initTitle() {
        this.setBarTitle(getString(R.string.record_major_hazard_name));
    }

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            data = (MajorHazardVo) bundle.getSerializable(KeyConstants.KEY_MAJOR_HAZARD_VO);
            isShowReturn = bundle.getBoolean("isShowReturn");
            companyId = bundle.getString("companyId");
        }
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(RecordMajorNewDetailActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_major_new_detail;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mDataList = new ArrayList<>();
        if (isShowReturn) {
            ivReturn.setVisibility(View.VISIBLE);
        } else {
            ivReturn.setVisibility(View.INVISIBLE);
        }
        tvTitle.setText(data.getMajor_danger_name());
        initData();
    }

    private void initData() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("companyId", companyId);
        params.put("parentId", data.getMajor_danger_id());
        params.put("pageNum","1");
        params.put("pageSize","");
        RecordServices.init().getMajorHazardDetailInfo(params);
    }

    @Override
    protected void initListener() {
        super.initListener();
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity(RecordMajorNewDetailActivity.this);
                AppManager.getAppManager().finishActivity(MajorHazardNewActivity.class);
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_SEARCH_MAJOR_HAZARD_DETAIL.equals(event.getTag())) {
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.show();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                mHUDUtil.hidden();
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    List<MajorHazardVo> majorHazardVo = (List<MajorHazardVo>) event.getObj();
                    initDetailData(majorHazardVo);
                }
            }
        }
    }

    private void initDetailData(List<MajorHazardVo> majorHazardVo) {
        mDataList.clear();
        if(majorHazardVo != null && majorHazardVo.size() > 0){
            MajorHazardVo majorVo=null;
            for(int i=0; i < majorHazardVo.size();i++){
                majorVo = majorHazardVo.get(i);
                if(majorVo.getType().equals("equipment")) {
                    GroupMajorHazardvo vo = new GroupMajorHazardvo();
                    vo.setChecked_regularly(CommonUtil.avoidNullString(majorVo.getChecked_regularly()));
                    vo.setMajor_danger_id(majorVo.getMajor_danger_id());
                    vo.setMajor_danger_name(CommonUtil.avoidNullString(majorVo.getMajor_danger_name()));
                    vo.setMajorHazardVoList(new ArrayList<MajorHazardVo>());
                    mDataList.add(vo);
                }
            }

            for(int j=0;j<majorHazardVo.size();j++){
                majorVo = majorHazardVo.get(j);
                for(int k=0;k<mDataList.size();k++){
                    if(majorVo.getParent_id().equals(mDataList.get(k).getMajor_danger_id())){
                        mDataList.get(k).getMajorHazardVoList().add(majorVo);
                    }
                }
            }
            adapter = new MajorExpandaleAdapter(this,mDataList);
            expandList.setAdapter(adapter);
        }
    }

}




